package com.geostar.constructionland.attachment.config;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.gridfs.GridFS;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;

/**
 * @author : yell
 * @Description: mongodb gridDBFS配置
 * @date : 2021/01/06 16:18
 */
@Configuration
public class GridFsConfig {
    @Value("${spring.data.mongodb.bucket}")
    String bucket;
    @Bean
    public GridFsTemplate getGridFsTemplate(MongoDbFactory dbFactory, MongoConverter converter){
        return new GridFsTemplate(dbFactory,converter,bucket);
    }

    @Bean
    public GridFSBucket getGridFSBucket(MongoDbFactory dbFactory){
        MongoDatabase db = dbFactory.getDb();
        return GridFSBuckets.create(db,bucket);
    }

    @Bean
    public GridFS getGridFS(MongoDbFactory dbFactory){
        return new GridFS(dbFactory.getLegacyDb(),bucket);
    }
}
